package com.jin.blog.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.jin.blog.core.BlogConstants;
import com.jin.blog.pojo.Article;
import com.jin.blog.pojo.Project;

public class ProjectController extends BaseController {
	public void index() {
		final Integer projectId = getParaToInt(0);
		Integer pageNum = getParaToInt("p", 1);
		Page<Article> page = Article.dao.paginateByCache("article", "projectId_" + projectId + "_" + pageNum, pageNum, BlogConstants.PAGE_SIZE, 
				"select * ", 
				"from article where finish = 1 and projectId = ? order by id desc",
				projectId); 
		Project project = CacheKit.get("article", "projectId_" + projectId, new IDataLoader() {
			@Override
			public Object load() {
				return Project.dao.findById(projectId);
			}
		}); 
		setAttr("page_news", page);
		
		List<Map<String, Object>>crumbs = new ArrayList<>();
		Map<String, Object>map = new HashMap<>();
		map.put("name", project.getStr("name"));
		crumbs.add(map);
		setAttr("crumbs", crumbs);

		getCommonData();
		render("index.html");
	}
}
